#include "cxglobal.h"
#include <string>
#include <fstream>
#include <time.h>
#include <boost/thread.hpp>

using std::ofstream;
using boost::mutex;
using std::string;


short               CxGlobal::s_portNumber[4]={6655,6655,6655,6655};
unsigned int        CxGlobal::s_refnumber=0;
std::string         CxGlobal::s_ipAddr[4]={"127.0.0.1",
                                           "127.0.0.1",
                                           "127.0.0.1",
                                           "127.0.0.1"};
std::string         CxGlobal::s_videoFilepaht="./Vedio";
std::string         CxGlobal::s_setFilepath="./Config";
std::string         CxGlobal::s_logFilepath="./Log";
std::string         CxGlobal::s_mobileAddr="Samsung i9308";
boost::mutex        CxGlobal::s_mu;
std::string         CxGlobal::s_databaseAddr="CxDataBase";
std::string         CxGlobal::s_databaseType="SQL Server";

CxGlobal::CxGlobal()
{
    s_refnumber++;
}

CxGlobal::~CxGlobal()
{
    --s_refnumber;
}

string                 CxGlobal::getIpaddr(int n)const
{
    boost::mutex::scoped_lock lock(s_mu);
    return string(s_ipAddr[n]);
}
short                       CxGlobal::getPortnumber(int   n)const
{
    boost::mutex::scoped_lock lock(s_mu);
    return s_portNumber[n];
}
string                 CxGlobal::getMobileaddr()const
{
    boost::mutex::scoped_lock lock(s_mu);
    return string(s_mobileAddr);
}
string                 CxGlobal::getVideofilepath()const
{
    boost::mutex::scoped_lock lock(s_mu);
    return string(s_videoFilepaht);
}

string                 CxGlobal::getlogFilepath()const
{
    boost::mutex::scoped_lock lock(s_mu);
    return string(s_logFilepath);
}

string                 CxGlobal::getsetFilepath()const
{
    boost::mutex::scoped_lock lock(s_mu);
    return string(s_setFilepath);
}

std::string                 CxGlobal::getdbAddr()const
{
    boost::mutex::scoped_lock lock(s_mu);
    return string(s_databaseAddr);
}
std::string                 CxGlobal::getdbType()const
{
    boost::mutex::scoped_lock lock(s_mu);
    return string(s_databaseType);
}



void                       CxGlobal::setIpAddr(int n ,std::string  addr)
{
    boost::mutex::scoped_lock lock(s_mu);
    s_ipAddr[n]=addr;
}

void                     CxGlobal::setPortnumber(int n,short number)
{
    boost::mutex::scoped_lock lock(s_mu);
    s_portNumber[n]=number;
}

void                       CxGlobal::setMobileaddr(std::string addr)
{
    boost::mutex::scoped_lock lock(s_mu);
    s_mobileAddr=addr;
}

void                       CxGlobal::setVideofilepath(std::string path)
{
    boost::mutex::scoped_lock lock(s_mu);
    s_videoFilepaht=path;
}


void                       CxGlobal::setlogFilepath(std::string path)
{
    boost::mutex::scoped_lock lock(s_mu);
    s_logFilepath=path;
}

void                       CxGlobal::setsetFilepath(std::string path)
{
    boost::mutex::scoped_lock lock(s_mu);
    s_setFilepath=path;
}


void                       CxGlobal::setdbAddr(std::string  addr)
{
    boost::mutex::scoped_lock lock(s_mu);
    s_databaseAddr=addr;
}


void                       CxGlobal::setdbType(std::string  type)
{
    boost::mutex::scoped_lock lock(s_mu);
    s_databaseType=type;
}



void                    CxGlobal::writeSetfile(int  n,int  in,int  out)
{
    std::ofstream fout(getlogFilepath()+"/count.log",std::ios::app);

    fout<<"通道"<<n<<'\n';
    fout<<"远程地址为：";
    fout<<getIpaddr(n).c_str()<<'\n';

    fout<<"时间为：";

    time_t t;
    time(&t);
    fout<<ctime(&t);

    fout<<"\nCar in:";
    char buffer[20];
    itoa(in,buffer,10);
    fout<<buffer<<'\n';

    itoa(out,buffer,10);
    fout<<"\nCar out:";
    fout<<buffer<<'\n';
}

